<?php

namespace App\Core\Traits;

trait HasStates
{
    /**
     * 设置state变量
     * @param string $state
     * @param string $value
     * @return $this
     */
    public function state(string $state, mixed $value): static
    {
        $this->states[$state] = $value;
        return $this;
    }

    /**
     * 设置state变量
     * @param array $states
     * @param bool $isMerge
     * @return $this
     */
    public function states(array $states, bool $isMerge = true): static
    {
        if ($isMerge){
            $this->states = array_merge($this->states, $states);
        }else{
            $this->states = $states;
        }
        return $this;
    }

    /**
     * 删除state变量
     * @param string $state
     * @return $this
     */
    public function removeState(string $state): static
    {
        if (isset($this->states[$state])){
            unset($this->states);
        }
        return $this;
    }

    /**
     * 清空 states 变量
     * @return $this
     */
    public function clearStates(): static
    {
        $this->states = [];
        return $this;
    }

    /**
     * 获取 states 变量
     * @param string|null $key
     * @return array|mixed
     */
    public function getStates(string $key = null): mixed
    {
        return $key?($this->states[$key]??null):$this->states;
    }
}
